#ifdef _CH_
#pragma package <opencv>
#endif

#define CV_NO_BACKWARD_COMPATIBILITY

#ifndef _EiC
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <ctype.h>
#include <stdio.h>
#endif

IplImage*
create_mae_log_polar_image(IplImage *opencv_log_polar_img)
{
	IplImage *upper_img, *lower_img, *upper_img_fliped_vertically;
	
	upper_img = cvCreateImage(cvSize(opencv_log_polar_img->width, opencv_log_polar_img->height / 2), IPL_DEPTH_8U, 3);
	lower_img = cvCreateImage(cvSize(opencv_log_polar_img->width, opencv_log_polar_img->height / 2), IPL_DEPTH_8U, 3);
	upper_img_fliped_vertically = cvCreateImage(cvSize(opencv_log_polar_img->width, opencv_log_polar_img->height / 2), IPL_DEPTH_8U, 3);
	
	// copia a metade superior de opencv_log_polar_img para upper_img e a metade inferior par a lower_img
	cvSetImageROI(opencv_log_polar_img, cvRect(0, 0, opencv_log_polar_img->width, opencv_log_polar_img->height / 2));
	cvCopy(opencv_log_polar_img, upper_img, NULL);
	cvSetImageROI(opencv_log_polar_img, cvRect(0, opencv_log_polar_img->height / 2, opencv_log_polar_img->width, opencv_log_polar_img->height));
	cvCopy(opencv_log_polar_img, lower_img, NULL);
	
	// gira a imagem 180 graus verticalmente
	cvFlip(upper_img, upper_img_fliped_vertically, 1);
	
	cvLine(upper_img_fliped_vertically, cvPoint(upper_img_fliped_vertically->width/2, 0), cvPoint(upper_img_fliped_vertically->width/2, upper_img_fliped_vertically->height), cvScalar(0, 255, 0, 0), 2, 1, 0);
	
	cvShowImage("upper img", upper_img);
	cvShowImage("lower img", lower_img);
	cvShowImage("upper img fliped", upper_img_fliped_vertically);
	cvWaitKey(-1);
	
	cvReleaseImage(&upper_img);
	cvReleaseImage(&lower_img);
	cvReleaseImage(&upper_img_fliped_vertically);
	
	return NULL;
}

int main( int argc, char** argv )
{
    CvCapture* capture = 0;
    IplImage*  log_polar_img = 0;
    IplImage*  lin_polar_img = 0;
    IplImage*  recovered_img = 0;

    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
    else if( argc == 2 )
        capture = cvCaptureFromAVI( argv[1] );

    if( !capture )
    {
        fprintf(stderr,"Could not initialize capturing...\n");
        fprintf(stderr,"Usage: %s <CAMERA_NUMBER>    , or \n       %s <VIDEO_FILE>\n",argv[0],argv[0]);
        return -1;
    }

    cvNamedWindow( "Linear-Polar", 0 );
    cvNamedWindow( "Log-Polar", 0 );
    cvNamedWindow( "Recovered image", 0 );

    cvMoveWindow( "Linear-Polar", 20,20 );
    cvMoveWindow( "Log-Polar", 700,20 );
    cvMoveWindow( "Recovered image", 20,700 );

    for(;;)
    {
        IplImage* frame = 0;

        frame = cvQueryFrame( capture );
        if( !frame )
            break;

        if( !log_polar_img )
        {
            log_polar_img = cvCreateImage( cvSize(frame->width,frame->height), IPL_DEPTH_8U, frame->nChannels );
            lin_polar_img = cvCreateImage( cvSize(frame->width,frame->height), IPL_DEPTH_8U, frame->nChannels );
            recovered_img = cvCreateImage( cvSize(frame->width,frame->height), IPL_DEPTH_8U, frame->nChannels );
        }

        cvLogPolar(frame,log_polar_img,cvPoint2D32f(frame->width >> 1,frame->height >> 1),70, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS);
        cvLinearPolar(frame,lin_polar_img,cvPoint2D32f(frame->width >> 1,frame->height >> 1),70, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS);

#if 1
	cvLogPolar(log_polar_img,recovered_img,cvPoint2D32f(frame->width >> 1,frame->height >> 1),70, CV_WARP_INVERSE_MAP+CV_INTER_LINEAR);
#else
        cvLinearPolar(lin_polar_img,recovered_img,cvPoint2D32f(frame->width >> 1,frame->height >> 1),70, CV_WARP_INVERSE_MAP+CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS);
#endif

        cvShowImage("Log-Polar", log_polar_img );
        cvShowImage("Linear-Polar", lin_polar_img );
        cvShowImage("Recovered image", recovered_img );

	create_mae_log_polar_image(log_polar_img);

        if( cvWaitKey(10) >= 0 )
            break;
    }

    cvReleaseCapture( &capture );
    cvDestroyWindow("Linear-Polar");
    cvDestroyWindow("Log-Polar");
    cvDestroyWindow("Recovered image");

    return 0;
}

#ifdef _EiC
main(1,"laplace.c");
#endif
